{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Prepare Anomaly Labels\n",
    "\n",
    "In this notebook we use the `labeled_anomalies.csv` file from the telemanom project\n",
    "and convert it to the CSV that we will later on use in Orion."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from orion.data import load_signal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "CSV_URL = 'https://github.com/khundman/telemanom/raw/master/labeled_anomalies.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(CSV_URL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>chan_id</th>\n",
       "      <th>spacecraft</th>\n",
       "      <th>anomaly_sequences</th>\n",
       "      <th>class</th>\n",
       "      <th>num_values</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>P-1</td>\n",
       "      <td>SMAP</td>\n",
       "      <td>[[1899, 2099], [4286, 4594], [3289, 3529]]</td>\n",
       "      <td>[contextual, contextual, contextual]</td>\n",
       "      <td>8505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>S-1</td>\n",
       "      <td>SMAP</td>\n",
       "      <td>[[5050, 5497]]</td>\n",
       "      <td>[point]</td>\n",
       "      <td>7331</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>E-1</td>\n",
       "      <td>SMAP</td>\n",
       "      <td>[[4750, 4780], [5360, 5836]]</td>\n",
       "      <td>[contextual, contextual]</td>\n",
       "      <td>8516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>E-2</td>\n",
       "      <td>SMAP</td>\n",
       "      <td>[[5348, 6745]]</td>\n",
       "      <td>[point]</td>\n",
       "      <td>8532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>E-3</td>\n",
       "      <td>SMAP</td>\n",
       "      <td>[[4844, 8124]]</td>\n",
       "      <td>[point]</td>\n",
       "      <td>8307</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  chan_id spacecraft                           anomaly_sequences  \\\n",
       "0     P-1       SMAP  [[1899, 2099], [4286, 4594], [3289, 3529]]   \n",
       "1     S-1       SMAP                              [[5050, 5497]]   \n",
       "2     E-1       SMAP                [[4750, 4780], [5360, 5836]]   \n",
       "3     E-2       SMAP                              [[5348, 6745]]   \n",
       "4     E-3       SMAP                              [[4844, 8124]]   \n",
       "\n",
       "                                  class  num_values  \n",
       "0  [contextual, contextual, contextual]        8505  \n",
       "1                               [point]        7331  \n",
       "2              [contextual, contextual]        8516  \n",
       "3                               [point]        8532  \n",
       "4                               [point]        8307  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "labels_data = list()\n",
    "\n",
    "for _, row in df.iterrows():\n",
    "    signal = row.chan_id\n",
    "    data = load_signal('csv/' + signal + '.csv')\n",
    "    test = data[-row.num_values:]\n",
    "    \n",
    "    events = list()\n",
    "    for start, end in json.loads(row.anomaly_sequences):\n",
    "        start_ts = test.iloc[start].timestamp.astype(int)\n",
    "        end_ts = test.iloc[end].timestamp.astype(int)\n",
    "        events.append([start_ts, end_ts])\n",
    "    \n",
    "    labels_data.append({\n",
    "        'signal': signal,\n",
    "        'events': events\n",
    "    })\n",
    "    \n",
    "labels = pd.DataFrame(labels_data)[['signal','events']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>signal</th>\n",
       "      <th>events</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>P-1</td>\n",
       "      <td>[[1325872800, 1330192800], [1377432000, 138408...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>S-1</td>\n",
       "      <td>[[1392768000, 1402423200]]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>E-1</td>\n",
       "      <td>[[1387627200, 1388275200], [1400803200, 141108...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>E-2</td>\n",
       "      <td>[[1400544000, 1430719200]]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>E-3</td>\n",
       "      <td>[[1389657600, 1460505600]]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  signal                                             events\n",
       "0    P-1  [[1325872800, 1330192800], [1377432000, 138408...\n",
       "1    S-1                         [[1392768000, 1402423200]]\n",
       "2    E-1  [[1387627200, 1388275200], [1400803200, 141108...\n",
       "3    E-2                         [[1400544000, 1430719200]]\n",
       "4    E-3                         [[1389657600, 1460505600]]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "labels.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels.to_csv('labels.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
